<?php

namespace Library;

use Library\Wx;
/**
 * 用户账户钱包操作类
 *
 * @author pufan
 */
class UserWallet {
    private $wallet_model;
    private $user_id;
    public function __construct() {
        $this->wallet_model = D('user_wallet');
        $this->user_id = $_SESSION['user_id'];
    }
    
    public function add_money($money) {    
        if ($this->user_id) {
            $where['real_user_id'] = $this->user_id;
            $result = $this->wallet_model->lock(true)->where($where)->setInc('money', $money);
            if ($result == FALSE) {
                \Think\Log::record('add_money fail userid:'. $this->user_id);
                \Think\Log::record('add_money fail arg:', json_encode($data));
                \Think\Log::record('add_money fail sql:', $this->wallet_model->getLastSql());
            }
            return $result;
        } 
        
        return FALSE;
    }

    public function reduce_money($money) {
        if ($this->user_id) {
            $where['real_user_id'] = $this->user_id;
            $result = $this->wallet_model->lock(true)->where($where)->setDec('money', $money);
            if ($result == FALSE) {
                \Think\Log::record('add_money fail userid:'. $this->user_id);
                \Think\Log::record('add_money fail arg:', json_encode($data));
                \Think\Log::record('add_money fail sql:', $this->wallet_model->getLastSql());
            }
            return $result;
        }

        return FALSE;
    }
    
    public function empty_money() {    
        if ($this->user_id) {
            $where['real_user_id'] = $this->user_id;
            $result = $this->wallet_model->lock(true)->where($where)->setField('money', 0);
            return $result;
        } 
        
        return FALSE;
    }
    
    public function get_user_money() {
        if ($this->user_id) {
            $where['real_user_id'] = $this->user_id;
            $result = $this->wallet_model->lock(true)->where($where)->getField('money');

            if (empty($result)) {
                $result = 0;
            }
            return $result;
        } 
        
        return FALSE;
    }
    
    /*
     * 微信用户提现接口
     */
    public function withdraw($userMoney = 0) {
        $openid = $_SESSION['open_id'];
        $money = $this->get_user_money();

        if ($userMoney > $money) {
            return 4;
        }
        $money = $userMoney;
        if ($money < 1000) {
            return 1;
        }
        if ($money > 20000) {
            $money = 20000;
        }

        $hb_tx = S('hb_tx:'.$_SESSION['open_id']);
        if ($hb_tx == true) {
            return 3;
        }
        S('hb_tx:'.$_SESSION['open_id'], 1, 60);

        if ($openid) {
           $hb['openid'] = $openid;
           $hb['send_name'] = '抱抱堂';
           $hb['act_name'] = '抱抱堂摇一摇';
           $hb['amount'] = $money;
           $hb['wishing'] = '提现红包';
           $hb['remark'] = '快乐了就吃抱抱堂';

            $wxPay =  new \Library\WxPay();
            if ($wxPay->sen_tx_hb($hb)) {
                $this->reduce_money($money);
                return 0;
            } 
        }
        return 2;
    }

    /**
     * @return mixed
     */
    public function getTxCountByCurMonth()
    {
        $sql = "SELECT 
                    COUNT(id) as  total
                FROM 
                    `yy_wx_pay_order` 
                WHERE 
                     date_format(time, '%Y-%m') = date_format(now(), '%Y-%m') 
                    AND `openid`='".$_SESSION['open_id']."'";
        $data = $this->wallet_model->query($sql);
        if ($data && is_array($data)) {
            if (isset($data[0]['total'])) {
                return $data[0]['total'];
            } else {
                return 0;
            }
        }
        return 0;
    }
}
